package Interface;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;

import javax.swing.tree.*;

/**
 *
 * @author v3550
 */
public class ModelTree extends JPanel {

    protected DefaultMutableTreeNode rootNode;
    
    protected DefaultTreeModel treeModel;
    
    protected JTree tree;
    private Toolkit toolkit = Toolkit.getDefaultToolkit();
    FormPartner  FormParent = null;
    public ModelTree(FormPartner form) {
        FormParent = form;
        setLayout(new FlowLayout(FlowLayout.LEFT));
        setBackground(Color.WHITE);
        rootNode = new DefaultMutableTreeNode("Lista de Tabelas");
        treeModel = new DefaultTreeModel(rootNode);
        tree = new JTree(treeModel);

        tree.getSelectionModel().setSelectionMode(
                TreeSelectionModel.SINGLE_TREE_SELECTION);
  
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);
        JScrollPane scrollPane = new JScrollPane(tree);
        scrollPane.setPreferredSize(new Dimension(220, 480));
        add(scrollPane);

       tree.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent me) {
                   doMouseClicked(me);
            }
       });


    }

    private void doMouseClicked(MouseEvent me) {
        try
        {    
        TreePath currentSelection = tree.getSelectionPath();  
        if (currentSelection != null) {  
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode)  
                         (currentSelection.getLastPathComponent());  
            MutableTreeNode parent = (MutableTreeNode)(currentNode.getParent());  
            if (parent != null && !tree.getModel().getRoot().toString().equals(parent.toString())) {  
                FormTable ft = new FormTable(parent.toString(),currentNode.toString());
                //FormPartner f = new FormPartner();
                Dimension dm = FormParent.getDimensionDesktopTable();
                ft.setBounds(0, 0, dm.width, dm.height);
                FormParent.setDesktopTable(ft);
                ft.setConteudo(parent.toString(), currentNode.toString());
                ft.setVisible(true);
                return;
            }  
        }
        }catch (Exception ex){
           JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /**
     * Remove all nodes except the root node.
     */
    public void clear() {
        rootNode.removeAllChildren();
        treeModel.reload();
    }

    /**
     * Remove the currently selected node.
     */
    public void removeCurrentNode() {
        TreePath currentSelection = tree.getSelectionPath();
        if (currentSelection != null) {
            DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection.getLastPathComponent());
            MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
            if (parent != null) {
                treeModel.removeNodeFromParent(currentNode);
                return;
            }
        }

        // Either there was no selection, or the root was selected.
        toolkit.beep();
    }

    /**
     * Add child to the currently selected node.
     */
    public DefaultMutableTreeNode addObject(Object child) {
        DefaultMutableTreeNode parentNode = null;
        TreePath parentPath = tree.getSelectionPath();

        if (parentPath == null) {
            parentNode = rootNode;
        } else {
            parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent());
        }

        return addObject(parentNode, child, true);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
            Object child) {
        return addObject(parent, child, false);
    }

    public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
            Object child, boolean shouldBeVisible) {
        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

        if (parent == null) {
            parent = rootNode;
        }

        // It is key to invoke this on the TreeModel, and NOT DefaultMutableTreeNode
        treeModel.insertNodeInto(childNode, parent, parent.getChildCount());

        // Make sure the user can see the lovely new node.
        if (shouldBeVisible) {
            tree.scrollPathToVisible(new TreePath(childNode.getPath()));
        }
        return childNode;
    }
}
